package org.lojzesframework.freeMarker.utils;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public abstract class FreeMakerUtils {
	static Configuration cfg = null;
	static String basePath = "templates";
	static {
		try {
			cfg = new Configuration(Configuration.VERSION_2_3_23);
			cfg.setDirectoryForTemplateLoading(new File(basePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Template getTemplate(String tplName) {
		try {
			return cfg.getTemplate(tplName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;  
	}
	
	public static String getContent(String tplName,Map map){
		Template template = getTemplate(tplName);
		StringWriter stringWriter = new StringWriter();
		try {
			template.process(map, stringWriter);
			stringWriter.flush();
			return stringWriter.toString();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				stringWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "";
	}
}
